/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.jjsjshop.com
 * 注意：
 * 本软件为www.jjsjshop.com开发研制，项目使用请保留此说明
 */
const app = getApp()

Page({
    data: {
        config: app.globalData.config,
        page: {
            searchCount: false,
            current: 1,
            size: 10
        },
        loadmore: true,
        goodsList: [],
        goodsListNew: [],
        goodsListHot: [],
        swiperData: [],
        cardCur: 0,
        noticeData: [],
        flashAd: null
    },
    onLoad() {
        app.initPage()
            .then(res => {
                this.loadData()
            })
    },
    onShow() {
        //更新tabbar购物车数量
        wx.setTabBarBadge({
            index: 2,
            text: app.globalData.shoppingCartCount + ''
        })
    },
    loadData() {
        this.mallHome()
        this.goodsNew()
        this.goodsHot()
        this.goodsPage()
    },
    onShareAppMessage: function () {
        let title = '金建商城'
        let path = 'pages/home/index'
        return {
            title: title,
            path: path,
            success: function (res) {
                if (res.errMsg == 'shareAppMessage:ok') {
                    console.log(res.errMsg)
                }
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    //新品首发
    goodsNew() {
        app.api.goodsPage({
            searchCount: false,
            current: 1,
            size: 5,
            descs: 'create_time'
        })
            .then(res => {
                let goodsListNew = res.data.records
                this.setData({
                    goodsListNew: goodsListNew
                })
            })
    },
    //热销单品
    goodsHot() {
        app.api.goodsPage({
            searchCount: false,
            current: 1,
            size: 5,
            descs: 'sale_num'
        })
            .then(res => {
                let goodsListHot = res.data.records
                this.setData({
                    goodsListHot: goodsListHot
                })
            })
    },
    goodsPage(e) {
        app.api.goodsPage(this.data.page)
            .then(res => {
                let goodsList = res.data.records
                this.setData({
                    goodsList: [...this.data.goodsList, ...goodsList]
                })
                if (goodsList.length < this.data.page.size) {
                    this.setData({
                        loadmore: false
                    })
                }
            })
    },
    mallHome() {
        app.api.mallHome().then(res => {
            const data = res.data;
            let ad = data.ad.records;
            const banner = data.banner.records;
            const notice = data.notice;
            let f = wx.getStorageSync('flashAd');
            let flash = null;
            ad.forEach((ele, index) => {
                if (f != null && f != undefined && f != "") {
                    if (f.indexOf(ele.bannerId) > -1) {
                        ad.splice(index, 1);
                    } else {
                        f.unshift(ele);
                    }
                } else if (f == "") {
                    f = []
                    f.push(ele.bannerId);
                } else {
                    f.push(ele.bannerId);
                }
            });
            if (ad.length > 0) {
                flash = ad[0]
            }
            wx.setStorageSync('flashAd', f);
            this.setData({
                noticeData: notice,
                swiperData: banner,
                flashAd: flash
            })
            if (flash) {
                setTimeout(() => {
                    this.setData({
                        flashAd: null
                    })
                }, 3000)
            }
        })
    },
    refresh() {
        this.setData({
            loadmore: true,
            ['page.current']: 1,
            goodsList: [],
            goodsListNew: [],
            goodsListHot: []
        })
        this.loadData()
    },
    onPullDownRefresh() {
        // 显示顶部刷新图标
        wx.showNavigationBarLoading()
        this.refresh()
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading()
        // 停止下拉动作
        wx.stopPullDownRefresh()
    },
    onReachBottom() {
        if (this.data.loadmore) {
            this.setData({
                ['page.current']: this.data.page.current + 1
            })
            this.goodsPage()
        }
    },
    jumpPage(e) {
        let page = e.currentTarget.dataset.page
        if (page) {
            wx.navigateTo({
                url: page
            })
        }
    },
    showNotice(e) {
        const id = e.currentTarget.dataset.id;
        const mallNotice = this.data.noticeData[id];
        wx.showModal({
            title: mallNotice.noticeType == "1" ? "通知" : "公告",
            content: mallNotice.noticeContent,
            showCancel: false,
            success: function (res) {
            }
        })
    },
    swiperImageTap(e) {
        const index = e.currentTarget.dataset.index;
        let image = this.data.swiperData[index];
        if (image && image.bannerLinkType == "0") {
            if (image.bannerLink.indexOf("goods") > 0 || image.bannerLink.indexOf("shopping-cart") > 0 || image.bannerLink.indexOf("user") > 0) {
                wx.switchTab({
                    url: image.bannerLink,
                })
            } else {
                wx.navigateTo({
                    url: image.bannerLink
                })
            }
        } else if (image && image.bannerLinkType == "1") {
            const bannerLink = image.bannerLink;
            wx.navigateTo({
                url: '/pages/web-view/index?bannerLink=' + bannerLink,
            })
        } else if (image && image.bannerLinkType == "2") {
            const bannerLink = image.bannerLink;
            wx.navigateTo({
                url: '/pages/goods/goods-detail/index?code=' + bannerLink,
            })
        }
    },
    closeFlashAd() {
        this.setData({
            flashAd: null
        })
    },
    change(e) {
        // console.log(e);
    },
})